<?php

/**
 * This is the model class for table "user_department".
 *
 * The followings are the available columns in table 'user_department':
 * @property string $id
 * @property integer $parent_id
 * @property integer $level
 * @property integer $project_group
 * @property string $name
 * @property string $manager_unique_id
 * @property string $manager
 * @property string $c_name
 * @property integer $status
 * @property integer $attribute
 * @property integer $dateline
 */
class UserDepartment extends CActiveRecord
{
	public $departmentPath = array();
	public $departmentTree = array();
	public $departmentChildren = array();

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UserDepartment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_department';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('parent_id, level, project_group, status, attribute, display, dateline', 'numerical', 'integerOnly'=>true),
			array('name, c_name', 'length', 'max'=>120),
			array('manager_unique_id, manager', 'length', 'max'=>64),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, parent_id, level, project_group, name, manager_unique_id, manager, c_name, status, attribute, display, dateline, admin_assistant, hr_assistant', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array();
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'parent_id' => 'Parent',
			'level' => 'Level',
			'project_group' => 'Project Group',
			'name' => 'Name',
			'manager_unique_id' => 'Manager Unique',
			'manager' => 'Manager',
			'c_name' => 'C Name',
			'status' => 'Status',
			'attribute' => 'Attribute',
			'display' => 'display',
			'dateline' => 'Dateline',
			'admin_assistant' => 'admin_assistant',
			'hr_assistant' => 'hr_assistant',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('parent_id',$this->parent_id);
		$criteria->compare('level',$this->level);
		$criteria->compare('project_group',$this->project_group);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('manager_unique_id',$this->manager_unique_id,true);
		$criteria->compare('manager',$this->manager,true);
		$criteria->compare('c_name',$this->c_name,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('attribute',$this->attribute);
		$criteria->compare('display',$this->display);
		$criteria->compare('dateline',$this->dateline);
		$criteria->compare('admin_assistant',$this->admin_assistant);
		$criteria->compare('hr_assistant',$this->hr_assistant);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	//路径
	public function getPath($id)
	{
		$res = $this->findByAttributes(array('id' => $id , 'status' => 1));
		if($res)
		{
			$arr['name'] = $res->name;
			$arr['level'] = $res->level;
			$arr['parent_id'] = $res->parent_id;
			$arr['manager_unique_id'] = $res->manager_unique_id;
			$arr['manager'] = $res->manager;
			$arr['id'] = $res->id;
			array_push($this->departmentPath,$arr);
			if($res->parent_id == 0) {

			} else {
				$this->getPath($res->parent_id);
			}
		}
		return null;
	}



	//树形结构
	public function getTree($id , $status=1 , $display=1)
	{
		if($display == 1)
			$res = $this->findAllByAttributes(array('parent_id' => $id , 'status' => $status ,'display'=>$display));
		else
			$res = $this->findAllByAttributes(array('parent_id' => $id , 'status' => $status));
		if($res)
		{	
			$arr = array();
			foreach ($res as $key => $value)
			{
				$arr['name'] = $value->name;
				$arr['level'] = $value->level;
				$arr['manager_unique_id'] = $value->manager_unique_id;
				$arr['manager'] = $value->manager;
				$arr['project_group'] = $value->project_group;
				$arr['attribute'] = $value->attribute;
				$arr['display'] = $value->display;
				$arr['admin_assistant'] = $value->admin_assistant;
				$arr['hr_assistant'] = $value->hr_assistant;
				$arr['id'] = $value->id;
				$this->departmentTree[] = $arr;
				$this->getTree($value->id, $status,$display);
			}
		}
		return null;
	}

	//获取ID的子部门
	public function getChildren($id)
	{
		$res = $this->findAllByAttributes(array('parent_id' => $id , 'status' => 1));
		if($res)
		{
			$arr = array();
			foreach ($res as $key => $value)
			{
				$arr['name'] = $value->name;
				$arr['level'] = $value->level;
				$arr['manager_unique_id'] = $value->manager_unique_id;
				$arr['manager'] = $value->manager;
				$arr['project_group'] = $value->project_group;
				$arr['attribute'] = $value->attribute;
				$arr['display'] = $value->display;
				$arr['admin_assistant'] = $value->admin_assistant;
				$arr['hr_assistant'] = $value->hr_assistant;
				$arr['id'] = $value->id;
				$this->departmentChildren[] = $arr;
				//$this->getTree($value->id);
			}
		}
		return null;

	}

	public function getOptionsByPid($pid = 0)
	{
		/*
		$sql = "SELECT * FROM `".$this->_table."` WHERE status=1 AND parent_id='".$pid."'";
		$temp = $this->_sdb->queryAll($sql);
		foreach($temp as $v) {
			$array[$v['id']] = $v['name'];
		}
		return $array;
		*/
		$arr[0] = '请选择';
		$res = $this->findAllByAttributes(array('parent_id' => $pid , 'status' => 1));
		if($res)
		{
			foreach($res as $v)
			{
				$arr[$v->id] = $v->name;
			}
			return $arr;
		}
		return $arr;
	}

	public function getOptions($display = 0)
	{
		$this->getTree(0 ,1 , $display);
		//$this->departmentTree;
		$arr[0] = '请选择';
		foreach ($this->departmentTree as $key => $value) {
			# code...
			$arr[$value['id']] = str_repeat(' ',($value['level'])).$value['name'];
		}
		return $arr;
	}


	public function getName($id)
	{
		return $this->findByPk($id);
	}

	/**
	* 是不是部门主管 status>0 manager=$username
	*/
	public function checkManager($uniqueId)
	{
		if($this->countByAttributes(array('status' => 1 , 'manager_unique_id' => $uniqueId )))
			return 1;
		else 
			return 0;
	}

	public function checkAdminAssistant($uniqueId)
	{
		if($this->countByAttributes(array('status' => 1 , 'admin_assistant' => $uniqueId )))
			return 1;
		else 
			return 0;
	}


	public function statusOption()
	{
		return Dinit::statusName();
	}

	public function attrOption()
	{
		return Tinit::attributeName();

	}

	public function ynOption()
	{
		return Dinit::ynName();

	}

	public function bonusOption()
	{


	}

	public function getInfoById($id,$column)
	{
		$temp = UserDepartment::model()->findByPk($id);
		return $temp->$column?$temp->$column:null;
	}



}